<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 用户虚拟操作
 */
class UserCoinOpLog extends Model
{
    protected $table = 'user_coin_op_log';
    const OP_TYPE_COST = 1;
    const OP_TYPE_GAIN = 2;
    const OP_TYPE = [
        self::OP_TYPE_COST => '系统扣币',
        self::OP_TYPE_GAIN => '系统补币',
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    /**
     * 添加
     */
    static function addRecord($userId = 0, $coin = 0, $opType = 1, $opReason = '', $adminId = 0)
    {
        $model = new self();
        $model->user_id = $userId;
        $model->coin = $coin;
        $model->op_type = $opType;
        $model->op_reason = $opReason;
        $model->admin_id = $adminId;
        $model->saveOrFail();
    }
}
